package com.pwx.student.web.controller;

import com.pwx.student.common.Const;
import com.pwx.student.common.ResponseResult;
import com.pwx.student.service.IRcSystemService;
import com.pwx.student.utils.TokenUtils;
import com.pwx.student.vo.input.LoginInputVo;
import com.pwx.student.vo.output.LoginOutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: SystemController
 * Author: saber
 * Date:2022/10/25 16:57
 */
@RestController
public class SystemController {

    @Autowired
    private IRcSystemService irss;

    @GetMapping("/index")
    public String showIndex(){
        return "欢迎来到后台系统";
    }


    @PostMapping("/login")
    public ResponseResult doLogin(LoginInputVo inputVo, HttpSession session){
        ResponseResult result = irss.login(inputVo);

        LoginOutputVo outputVo =(LoginOutputVo) result.getData();


        if (outputVo!=null){

            session.setAttribute(Const.IS_LOGIN,outputVo);

            String token = TokenUtils.getToken(outputVo,Const.HALF_HOUR);

            result.setData(token);
        }

        return result;
    }


} 
